package com.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.domain.Bookshelf;
import com.example.dto.BookShelfAddDTO;
import com.example.dto.BookShelfUpdateDTO;
import com.example.mapper.BookshelfMapper;
import com.example.service.BookshelfService;
import com.example.util.Result;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
* @author HP
* @description 针对表【bookshelf】的数据库操作Service实现
* @createDate 2025-08-25 20:58:02
*/

@Slf4j
@Service
public class BookshelfServiceImpl extends ServiceImpl<BookshelfMapper, Bookshelf>
    implements BookshelfService{

    @Resource
    private BookshelfMapper bookshelfMapper;


    @Override
    public Result addBookShelf(BookShelfAddDTO bookShelfAddDTO) {
        Bookshelf bookshelf = new Bookshelf();
        BeanUtils.copyProperties(bookShelfAddDTO, bookshelf);
        int result =  bookshelfMapper.insert(bookshelf);
        if (result != 1) {
            return Result.error("添加失败");
        }
        log.info("id为{}的书架添加成功", bookshelf.getId());
        return Result.success(bookshelf.getId());
    }

    @Override
    public Result deleteBookShelf(Long id) {
        int result =  bookshelfMapper.deleteById(id);
        if (result != 1) {
            return Result.error("删除失败");
        }
        return Result.success();
    }

    @Override
    public Result updateBookShelf(BookShelfUpdateDTO bookShelfUpdateDTO) {
        Bookshelf bookshelf = new Bookshelf();
        BeanUtils.copyProperties(bookShelfUpdateDTO, bookshelf);
        int result = bookshelfMapper.updateById(bookshelf);
        if (result != 1) {
            return Result.error("更新失败");
        }
        return Result.success();
    }
}




